/**
*栏目关联操作
*/

var cLink = {
	kdialog:null,	
	loadSource:function(first){
		_waiting._show('数据加载中....');
		$.ajax({
			url:'columnLinkSiteSource',
			type:'post',
			dataType:'json',
			success:function(data){
				var html = '';
				$.each(data.source,function(i,u){
					var line = '<div id="clink_id'+ u.colId +'" style="min-height:25px;verticle-align:middle;line-height:25px;border:1px solid #ccc;padding-left:5px;margin:3px;	"><b>'+ u.linkWebSiteName + '--' + u.linkColName + '</b><font size="2">已关联到</font><b>' + u.webSiteName + '--' + u.columnName  + '</b>'
							 + '<input id="'+i+'" type="checkbox" name="col_link" onclick="cLink.backgroundDiv(this)" value="'+u.colId+'" data="'+ u.linkcolId +'" style="float:right;margin-right:5px;margin-top:6px;"/></div>';
					html += line;
				});
				if(first)
					cLink.dialog(html);
				else
					$("#col_source_div").html(html);
				_waiting._hide();
			},
			error:function(msg){
				_waiting._hide();
				$.growlUI('数据加载出错');
			}
		});
		
	},
	dialog:function(html){
			this.kdialog = KindEditor.dialog({
			width : 600,
			height: 450,
			title : '列表',
			body : '<div id="col_source_div" style="padding:10px;height:300px;overflow:auto;overflow-x:hidden;">'+html+'</div>',
			shadowMode:true,
			closeBtn : {
				name : '关闭',
				click : function(e) {
					cLink.kdialog.remove();
				}
			},
			yesBtn : {
				name : '解除关联',
				click : function(e) {
					var checkArr = $("input[type='checkbox'][name='col_link']:checked");
					if(checkArr.size() > 0){
						var cidStr = '',linkcidId = '';
						
						checkArr.each(function(){
							cidStr += ';' + $(this).val();
							linkcidId += ';' + $(this).attr('data');
						});
						$.ajax({
							url:'cancleColumnLinkForSite',
							type:'post',
							dataType:'json',
							data:{'cidStr':cidStr,'linkStr':linkcidId},
							success:function(data){
								if(data > 0){
									$.growlUI('已解除关联');
									cLink.loadSource(false);
								}else{
									$.growlUI('解除失败');
								}
							},
							error:function(msg){
								$.growlUI('解除出错');
							}
						});
					}else{
						$.growlUI('未选择任何选项');
					}
				}
			},
			noBtn : {
				name:'取消',
				click:function(e){
					cLink.kdialog.remove();
				}
			}
		});
	},
	backgroundDiv:function(obj){
		if(obj.checked){
			$("#clink_id" + obj.value).css('background','#ccc');
		}else{
			$("#clink_id" + obj.value).css('background','#ffffff');
		}
	}
};
//栏目复制
var copyClumn = {
	kdialog : null,
	loadClumn : function(local){
		if(liObj != null){
			var html = '';
			var divS = '<div id="tcol_dataGrid" style="padding:10px;height:350px;overflow:auto;overflow-x:hidden;">';
			var divE = '</div>';
		
			$.ajax({
				url:'websiteinfoSource',
				type:'post',
				dataType:'json',
				success:function(data){
						html += '选择网站:';
						html += '<select id="linkColumn_web" onchange="TcolunmSource(this,\''+local+'\')" style="width:150px;font-size:14px;color:#5E5E5E;margin-top:-3px;" ><option value="" disabled="disabled" selected="selected">----站点列表----</option>';
					$.each(data,function(i,u){
						html += '<option value="'+u.id+'">'+u.ftitle+'</option>';
					});
					html += '</select>\
							<input id="C_WEB_NAME" type="text" onkeyup="extendArticle.addDivPanel(this,\'TcolunmSource(this,null,true)\')" onblur="extendArticle.delDivPanel()"  style="margin-left:-156px;font-size:14px; position: relative;width:130px;"/>\
							<input style="margin-left:30px;" type="radio" name="_col_lv" value="0" />同级栏目<input type="radio" name="_col_lv" value="1" checked="checked"/>子栏目';
						
				
					html += divS + divE;
					copyClumn.dialog(html);
				},
				error:function(msg){
					$.growlUI('网站数据加载出错');
				}
			});
		}else{
			$.growlUI("请选择源栏目");
		}
	},
	dialog : function(html){
		this.kdialog = KindEditor.dialog({
			width : 600,
			height: 450,
			title : '目标网站/栏目 <font size="2" color="#f4865a">[复制栏目及其所关联的文章]</font>',
			body : '<div id="col_source_div" style="padding:10px;height:455px;overflow:auto;overflow-x:hidden;">'+html+'</div>',
			shadowMode:true,
			closeBtn : {
				name : '关闭',
				click : function(e) {
					copyClumn.kdialog.remove();
				}
			},
			yesBtn : {
				name : '确定',
				click : function(e) {
					var carr = $("input[type='checkbox'][name='columnId']:checked");
					if(carr.size() > 0){
						
						_waiting._show('正在复制栏目...');
						
						var tarcid = '';
						carr.each(function(){
							tarcid += ';' + $(this).val();
						});
						$.ajax({
							url:'copyColumnsSource',
							type:'post',
							dataType:'json',
							data:{'sourid':liObj.value,'cidStr':tarcid,'swebid':$("#select_web").val(),'webid':$("#linkColumn_web").val(),'type':$("input[type='radio'][name='_col_lv']:checked").val()},
							success:function(data){
								if(data > 0){
									$.growlUI('栏目已复制');
								}else{
									$.growlUI('栏目复制失败');
								}
								_waiting._hide();
							},
							error:function(msg){
								$.growlUI('复制出错');
								_waiting._hide();
							}
						});
					}else{
						$.growlUI('目标栏目未选中');
					}
				}
			},
			noBtn : {
				name:'取消',
				click:function(e){
					copyClumn.kdialog.remove();
				}
			}
		});
	}
};
var fileUpLoad = {
		nowFilePath : '',
		uploadDialog : function(){
			var html = '<form id="upload_form"  method="post" enctype="multipart/form-data">\
						选取文件：<input id="pakage" type="file" name="file" onchange="fileUpLoad.validateSuffix(this)"/><input type="button" onclick="fileUpLoad.upload();" value=" 确定上传  "/><br/>(提示:上传之前请先打包成<b>zip/rar</b>格式文件再上传，目前仅限<b>zip/rar</b>格式的文件上传)\
						</form><div class="_loading"></div><div class="tip_show"><img  src="/CMS/images/tree/statusbar_loading.gif" /><font  id="tip_show">正在处理，请稍后....</font></div>';
			var kdialog = KindEditor.dialog({
				width : 500,
				height: 400,
				title : '文件上传 <font size="2" color="#f4865a">[支持自定义静态页面上传]</font>',
				body : '<div id="upload_source_div" style="padding:10px;height:305px;overflow:auto;overflow-x:hidden;">'+html+'</div>',
				shadowMode:true,
				closeBtn : {
					name : '关闭',
					click : function(e) {
						kdialog.remove();
					}
				},
				noBtn : {
					name:'取消',
					click:function(e){
						kdialog.remove();
					}
				}
			});
		},
		upload : function(){
			if($("#pakage").val() != ''){
				$("._loading").fadeIn(100);
				$(".tip_show").show();
				$("#upload_form").ajaxSubmit({
					url:'uploadStaticFile',
					type:'post',
					dataType:'json',
					success:function(data){
						if(data.mess == 1){
							$("#tip_show").html("上传成功，正在解压缩....");
							setTimeout(function(){
								fileUpLoad.unPakege(data.filename);
							},2000);
						}else{
							$("#tip_show").html(data.mess);
							setTimeout(function(){
								$("._loading").fadeOut(100);
								$(".tip_show").hide();
							},2000);
						}
					},
					error:function(msg){
						$("#tip_show").html(msg.mess);
						$("._loading").fadeOut(100);
						$(".tip_show").hide();
					}
				});
			}
		},
		unPakege : function(fileName){
			$.ajax({
				url:'unPakage',
				type:'post',
				dataType:'json',
				data:{'filePath':fileName},
				success:function(data){
					$.growlUI(data.mess);
					$("._loading").fadeOut(100);
					$(".tip_show").hide();
					$("#pakage").val('');
				},
				error:function(msg){
					$.growlUI(msg.mess);
					$("._loading").fadeOut(100);
					$(".tip_show").hide();
				}
			});
		},
		validateSuffix : function(obj){
			var filename = obj.value,
				suffix = filename.substring(filename.lastIndexOf(".") + 1,filename.length).toLowerCase();
			
			if(suffix != "zip" && suffix != "rar"){
				obj.value = "";
				$.growlUI("文件格式不正确");
			}
		},
		changeFold : function(path,fold,first){
			if(!fold){
				this.nowFilePath += "/" + path;
			}else{
				this.nowFilePath = path;
			}
			
			var pathArr = this.nowFilePath.split("/");
			var html = '',aHtml = '<a href="javascript:void(0);" onclick="fileUpLoad.changeFold(\'\',true,true);"> / </a> ';
			for(var i = 0;i < pathArr.length; i++){
				if(pathArr[i] != ''){
					html += '/' + pathArr[i];
					aHtml += '<a href="javascript:void(0);" onclick="fileUpLoad.changeFold(\''+html+'\',true)">'+pathArr[i]+'</a> / ';
				}
			}
			
			$(".filepath").html(aHtml);
			this.fileList(this.nowFilePath,first);
		},
		fileList : function(filename,first){
			var suffix = '';
			$.ajax({
				url:'staticFileList',
				type:'post',
				dataType:'json',
				data:{'path':filename},
				success:function(data){
					var html = '';
					$.each(data.list,function(i,u){
						if(u.isfold){
							html += '<div class="file"><a href="javascript:void(0)" style="text-decoration: underline;" onclick="fileUpLoad.changeFold(\''+u.filename+'\',false)">' +u.filename+ '</a>';
							if(first)
								html += '<a class="delFold" style="color:red;" href="javascript:void(0);" onclick="fileUpLoad.delFold(\''+u.filename+'\');" title="删除文件夹">删   除</a></div>';
						}else{
							html += '<div class="file">' +u.filename+ '</div>';
							suffix = u.filename.substring(u.filename.lastIndexOf("."),u.filename.length);
							if(suffix.toLowerCase().indexOf('html') != -1 || suffix.toLowerCase().indexOf('php') != -1){
								html += '<a class="prew" style="color:blue;" href="/CMS/uploadStaticFiles'+fileUpLoad.nowFilePath + "/" + u.filename +'" target="_blank" title="新窗口预览">预  览</a>';
							}
						}
					});
					$(".fileslist").html(html);
					$(".file:odd").css('background','#EBEBEB');
				},
				error:function(msg){
					$.growlUI("列表加载出错");
				}
			});
		},
		fileListDialog : function(){
			this.nowFilePath = '';
			var kdialog = KindEditor.dialog({
				width : 700,
				height: 500,
				title : '文件列表 <font size="2" color="#f4865a">[自定义静态文件列表]</font>',
				body : '<div id="upload_source_div" style="padding:10px;height:405px;overflow:hidden;"><div class="filepath">/</div><div class="fileslist"></div></div>',
				shadowMode:true,
				closeBtn : {
					name : '关闭',
					click : function(e) {
						kdialog.remove();
					}
				},
				noBtn : {
					name:'取消',
					click:function(e){
						kdialog.remove();
					}
				}
			});
			
			this.fileList("",true);
		},
		delFold : function(foldname){
			if(foldname != ''){
				if(confirm("确定删除？")){
					_waiting._show("正在执行操作，请稍后");
					$.ajax({
						url:'deleteStaticFile',
						type:'post',
						dataType:'json',
						data:{'filename':foldname},
						success:function(data){
							setTimeout(function(){
								_waiting._hide();
								$.growlUI('删除' + (data == 0?'成功':'失败'));
								if(data == 0)
									fileUpLoad.fileList("", true);
							},2000);
						},
						error:function(msg){
							$.growlUI('删除出错');
							_waiting._hide();
						}
					});
				}
			}else{
				$.growlUI('请选择删除目录');
			}
		}
};

